/**
* ...
* @author Default
* @version 0.1
* 
* Information:This move the image in key, array, and timer. It can go up to three keys for movent keys.
* 
* Error: If you press the keys up,down,right,left more at the smae time it will beep.
* 
*/


/**
* @mxmlc -source-path=C:\\Program Files\\FlashDevelop\\FirstRun\\Library\\AS3\\classes\\
*/

package  {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.ui.Keyboard;
	import org.flashdevelop.utils.FlashConnect;

	public class keymoveimage00 extends Sprite{
		
		public function keymoveimage00() {
			KEY_EVENT();
		}
		public function KEY_EVENT():void{
			var KB_RIGHT:Boolean = false;
			var KB_LEFT:Boolean = false;
			var KB_DOWN:Boolean = false;
			var KB_UP:Boolean = false;
			var keysDown:Array = new Array();
			
			var image_url:String = "imagejpg.jpg";
			var imagetest:Loader = new Loader();
			var request:URLRequest = new URLRequest(image_url);
			imagetest.load(request);
			addChild(imagetest);
			
			
			var key:TextField = new TextField();
			key.width = 200;
			key.text = "Key:[R]" + KB_RIGHT + "[L]" + KB_LEFT + "[U]" + KB_UP + "[D]" + KB_DOWN;
			addChild(key);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, addKey, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_UP, removeKey, false, 0, true);
			
			function addKey(e:KeyboardEvent):void {
				keysDown[e.keyCode] = true;
				FlashConnect.trace(String(keysDown[e.keyCode]));
				//FlashConnect.trace("addKey!");
			}
			function removeKey(e:KeyboardEvent):void {
				keysDown[e.keyCode] = false;
				FlashConnect.trace(String(keysDown[e.keyCode]));
				//FlashConnect.trace("removeKey");
			}
			// here's how you'd implement it:
			var t:Timer = new Timer(25);
			t.start();
			t.addEventListener(TimerEvent.TIMER, tick, false, 0, true);
			
			function tick(e:TimerEvent):void {
				//FlashConnect.trace("tick");
				// old way- if (Key.isDown(Key.RIGHT)) {
				if (keysDown[Keyboard.RIGHT]) {
					//FlashConnect.trace("right key is down!");
					KB_RIGHT = true;
					imagetest.x = imagetest.x + 1;
				}else {
					KB_RIGHT = false;
				}
				
				if (keysDown[Keyboard.LEFT]) {
					//FlashConnect.trace("Left key is down!");
					KB_LEFT = true;
					imagetest.x = imagetest.x - 1;
				}else{
					KB_LEFT = false;
					
				}
				if (keysDown[Keyboard.UP]) {
					//FlashConnect.trace("Up key is down!");
					KB_UP = true;
					imagetest.y = imagetest.y - 1;
				}else{
					KB_UP = false;
				}
				if (keysDown[Keyboard.DOWN]) {
					//FlashConnect.trace("Down key is down!");
					KB_DOWN = true;
					imagetest.y = imagetest.y + 1;
				}else{
					KB_DOWN = false;
					
				}
				key.text = "Key:[R]" + KB_RIGHT + "[L]" + KB_LEFT + "[U]" + KB_UP + "[D]" + KB_DOWN;
			// etc.
			}			
		}		
		
	}
	
}
